package com.alan.wifishare.presenter;

import com.alan.libapi.api.ApiResponse;
import com.alan.libapi.business.dao.CallbackListener;
import com.alan.libapi.model.business.ConnectedDeviceInfo;
import com.alan.libapi.model.business.TcpResponseStatus;
import com.alan.library.allog.KLog;
import com.alan.wifishare.app.GlobalField;
import com.alan.wifishare.view.iview.IVCNTManageConnectedDevices;

import java.util.List;

/**
 * @className: CNTManageConnectedDevicesPS
 * @describtion: CNTManageConnectedDevicesPS
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/4/26 20:09
 */
public class CNTManageConnectedDevicesPS {

    private IVCNTManageConnectedDevices ivcntManageConnectedDevices;
    private List<ConnectedDeviceInfo> connectedDeviceList;

    public CNTManageConnectedDevicesPS(IVCNTManageConnectedDevices ivcntManageConnectedDevices) {
        this.ivcntManageConnectedDevices = ivcntManageConnectedDevices;
    }

    public void getConnectedDevices(){
        // 与Link通信获取已连接到link的设备列表
        GlobalField.businessTcp.getDeviceUserList(GlobalField.userSession.getUserId(), new CallbackListener<List<ConnectedDeviceInfo>>() {
            @Override
            public void onSuccess(final List<ConnectedDeviceInfo> result) {
                KLog.e(result);
                if(result != null){
                    connectedDeviceList = result;
                }
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        ivcntManageConnectedDevices.displayConnectedDevices(connectedDeviceList);
                    }
                });
            }
            @Override
            public void onFailure(int errorCode, String msg) {
                KLog.e(msg);
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {

                    }
                });
            }
        });
    }

    /**
     * 移除连接的设备
     * @param connectedDevice
     */
    public void moveOutConnectedDevice(final ConnectedDeviceInfo connectedDevice){
        // 跟Link通信踢除某一连接的设备
        GlobalField.businessTcp.deleteDeviceUser(GlobalField.userSession.getUserId(), connectedDevice.getDeviceMac(), new CallbackListener<TcpResponseStatus>() {
            @Override
            public void onSuccess(TcpResponseStatus result) {
                if(result.getStatus() == ApiResponse.SUCCESS_CODE){
                    GlobalField.handler.post(new Runnable() {
                        @Override
                        public void run() {
                            if(connectedDeviceList != null && connectedDeviceList.contains(connectedDevice)){
                                connectedDeviceList.remove(connectedDevice);
                                ivcntManageConnectedDevices.displayConnectedDevices(connectedDeviceList);
                            }
                        }
                    });
                }
            }

            @Override
            public void onFailure(int errorCode, String msg) {
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {

                    }
                });
            }
        });
    }
}
